using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using net_work.Data.Entity.Appr;
using net_work.Data.Enum.Appr;
using net_work.Data.Enum.System;

namespace net_work.Config.DataBaseConfig.Appr;

/// <summary>
/// 流程实体配置类
/// </summary>
public class FlowEntityTypeConfiguration : IEntityTypeConfiguration<Flow>
{
    public void Configure(EntityTypeBuilder<Flow> builder)
    {
        builder.ToTable("appr_flow");

        builder.HasKey(f => f.Id);

        // 配置字段名映射
        builder.Property(f => f.Id).HasColumnName("flow_id");
        builder.Property(f => f.Name).HasColumnName("flow_name").IsRequired().HasMaxLength(64);
        builder.Property(f => f.Description).HasColumnName("flow_desc").HasMaxLength(256);
        builder.Property(f => f.BusinessCode)
            .HasColumnName("flow_bns_code")
            .IsRequired()
            .HasMaxLength(64)
            .HasConversion(
                f => f.ToString(),
                v => (BusinessType)Enum.Parse(typeof(BusinessType), v)
            );

        // 将 FlowStatus 枚举存储为字符串
        builder.Property(f => f.Status)
            .HasColumnName("flow_status")
            .IsRequired()
            .HasDefaultValue(FlowStatus.UnRelease)
            .HasMaxLength(64)
            .HasConversion(
                v => v.ToString(), // 枚举转字符串
                v => (FlowStatus)Enum.Parse(typeof(FlowStatus), v) // 字符串转枚举
            );

        builder.Property(f => f.BeginFlowNodeId).HasColumnName("begin_flow_node_id");

        builder.Property(f => f.CreatedBy).HasColumnName("create_by").IsRequired().HasMaxLength(64);
        builder.Property(f => f.CreatedTime).HasColumnName("create_time").IsRequired();
        builder.Property(f => f.UpdatedBy).HasColumnName("update_by").IsRequired().HasMaxLength(64);
        builder.Property(f => f.UpdatedTime).HasColumnName("update_time").IsRequired();
        builder.Property(f => f.Remark).HasColumnName("remark").HasMaxLength(256);
        builder.Property(f => f.DelFlag).HasColumnName("del_flag").HasDefaultValue('0');
    }
}